//
//  NoteViewController.m
//  Grammar
//
//  Created by Rick on 6/25/13.
//
//

#import "NoteViewController.h"
#import "Grammar.h"
#import "GrammarManager.h"
#import "SearchResultCell.h"
#import "ITTXibViewUtils.h"
#import "NoteBookManager.h"
#import "KnowledgeDetailViewController.h"
#import "AppDelegate.h"


@interface NoteViewController ()<UITableViewDataSource,UITableViewDelegate,SearchResultCellDelegate,UIActionSheetDelegate>
{
    NSMutableArray  *_noteArray;
    NoteTableViewStyle _tableViewType;
    NSInteger _deleteIndex;
}
@property (retain, nonatomic) IBOutlet UIButton *reviewButton;
@property (retain, nonatomic) IBOutlet UIButton *archiveButton;
@property (retain, nonatomic) IBOutlet UIImageView *selectImageView;
@property (retain, nonatomic) IBOutlet UITableView *noteTableView;

@end

@implementation NoteViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    _tableViewType = NoteTableViewStyleStudy;
    [GrammarManager sharedManager].grammarType = @"review";
    [self performSelector:@selector(tapReviewButton:)];
}
- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    [[AppDelegate shareAppdelegate].homeTabbarController showTabBarAnimated:YES];
    [self updateButtonView];

    if (_noteArray) {
        RELEASE_SAFELY(_noteArray);
    }
    
    if (_tableViewType == NoteTableViewStyleNote) {
        [GrammarManager sharedManager].grammarType = @"archive";
        _noteArray =[[NSMutableArray alloc] initWithArray:[GrammarManager sharedManager].grammars];
    }else {
        [GrammarManager sharedManager].grammarType = @"review";
        _noteArray =[[NSMutableArray alloc] initWithArray:[GrammarManager sharedManager].grammars];
    }
    
    [_noteTableView reloadData];
}
- (void)updateButtonView
{
    [GrammarManager sharedManager].grammarType = @"review";
    [_reviewButton setTitle:[NSString stringWithFormat:@"待复习/答疑[%d]",[[GrammarManager sharedManager].grammars count]] forState:UIControlStateNormal];
    [GrammarManager sharedManager].grammarType = @"archive";
    [_archiveButton setTitle:[NSString stringWithFormat:@"归档[%d]",[[GrammarManager sharedManager].grammars count]] forState:UIControlStateNormal];
    if (_tableViewType == NoteTableViewStyleStudy) {
        [GrammarManager sharedManager].grammarType = @"review";
    }
}
- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)dealloc {
    [_noteArray release];
    [_reviewButton release];
    [_archiveButton release];
    [_selectImageView release];
    [_noteTableView release];
    [super dealloc];
}
- (void)sliderImageWithOffset:(CGFloat)offset
{
    [UIView animateWithDuration:0.3f animations:^{
        _selectImageView.left = offset;
    }];
}
#pragma mark -IBAction
- (IBAction)tapReviewButton:(id)sender {
    if (_noteArray) {
        RELEASE_SAFELY(_noteArray);
    }
    _tableViewType = NoteTableViewStyleStudy;
    
    [GrammarManager sharedManager].grammarType = @"review";
    _noteArray =[[NSMutableArray alloc] initWithArray:[GrammarManager sharedManager].grammars];
    [_noteTableView reloadData];
    
    [self sliderImageWithOffset:0];
}
- (IBAction)tapArchiveButton:(id)sender {
    if (_noteArray) {
        RELEASE_SAFELY(_noteArray);
    }
    
    _tableViewType = NoteTableViewStyleNote;
    
    [GrammarManager sharedManager].grammarType = @"archive";
    _noteArray =[[NSMutableArray alloc] initWithArray:[GrammarManager sharedManager].grammars];
    [_noteTableView reloadData];
    
    [self sliderImageWithOffset:160];
}
#pragma mark - UITableView
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return [_noteArray count];
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 81;
}
- (UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSString *identifer = @"SearchResultCell";
    SearchResultCell *cell = [tableView dequeueReusableCellWithIdentifier:identifer];
    if (!cell) {
        cell = [ITTXibViewUtils loadViewFromXibNamed:identifer];
    }
    [cell setCellWithGrammar:[_noteArray objectAtIndex:indexPath.row]];
    if (indexPath.row %2) {
        cell.contentView.backgroundColor = [UIColor colorWithRed:234/255.0 green:228/255.0 blue:218/255.0 alpha:1.0];
    }else {
        cell.contentView.backgroundColor = [UIColor colorWithRed:217/255.0 green:208/255.0 blue:195/255.0 alpha:1.0];
    }
    cell.delegate = self;
    cell.cellRow = indexPath.row;
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    return cell;
}


#pragma  mark - 
- (void)tapMoreOption:(Grammar *)grammar withIndex:(NSInteger)index
{
    _deleteIndex = index;
    
    if (_tableViewType == NoteTableViewStyleStudy) {
        
        UIActionSheet *actionSheet = [[UIActionSheet alloc] initWithTitle:nil delegate:self cancelButtonTitle:@"取消" destructiveButtonTitle:nil otherButtonTitles:@"归档",@"删除", nil];
        actionSheet.tag = 1000;
        [actionSheet showInView:self.view];
        [actionSheet release];
        
           
    }else if (_tableViewType == NoteTableViewStyleNote) {
        UIActionSheet *actionSheet = [[UIActionSheet alloc] initWithTitle:nil delegate:self cancelButtonTitle:@"取消" destructiveButtonTitle:nil otherButtonTitles:@"待复习/答疑",@"删除", nil];
        actionSheet.tag = 1001;
        [actionSheet showInView:self.view];
        [actionSheet release];
        
            
    }
}
- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex
{
    if (actionSheet.tag == 1000) {
        switch (buttonIndex) {
            case 0:
            {
                Grammar *knowledge = [[GrammarManager sharedManager].grammars objectAtIndex:_deleteIndex];
                [[GrammarManager sharedManager] removeGrammar:knowledge];
                [self archiveKnowledgeWithKnowledge:knowledge];
                
                [_noteArray removeObjectAtIndex:_deleteIndex];
                [_noteTableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:[NSIndexPath indexPathForRow:_deleteIndex inSection:0]] withRowAnimation:UITableViewRowAnimationFade];
                [self updateButtonView];
            }
                break;
            case 1:
            {
                Grammar *knowledge = [[GrammarManager sharedManager].grammars objectAtIndex:_deleteIndex];
                [[GrammarManager sharedManager] removeGrammar:knowledge];
                [self reviewKnowledgeWithGrammar:knowledge];
                
                [NoteBookManager sharedManager].gramarId = knowledge.grammarId;
                [[NoteBookManager sharedManager] removeNoteByGrammarId:knowledge.grammarId];
                [[GrammarManager sharedManager] removeGrammar:knowledge];
                [_noteArray removeObjectAtIndex:_deleteIndex];
                [_noteTableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:[NSIndexPath indexPathForRow:_deleteIndex inSection:0]] withRowAnimation:UITableViewRowAnimationFade];
                [self updateButtonView];
            }
                break;
            default:
                break;
        }
        
    }else if (actionSheet.tag == 1001) {
        switch (buttonIndex) {
            case 0:
            {
                Grammar *knowledge = [[GrammarManager sharedManager].grammars objectAtIndex:_deleteIndex];
                [[GrammarManager sharedManager] removeGrammar:knowledge];
                [self reviewKnowledgeWithGrammar:knowledge];
                
//                [NoteBookManager sharedManager].gramarId = knowledge.grammarId;
//                [[NoteBookManager sharedManager] removeNoteByGrammarId:knowledge.grammarId];
                [_noteArray removeObjectAtIndex:_deleteIndex];
                [_noteTableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:[NSIndexPath indexPathForRow:_deleteIndex inSection:0]] withRowAnimation:UITableViewRowAnimationFade];
                [self updateButtonView];
            }
                break;
            case 1:
            {
                Grammar *knowledge = [[GrammarManager sharedManager].grammars objectAtIndex:_deleteIndex];
                [[GrammarManager sharedManager] removeGrammar:knowledge];
//                [self reviewKnowledgeWithGrammar:knowledge];
                
                [NoteBookManager sharedManager].gramarId = knowledge.grammarId;
                [[NoteBookManager sharedManager] removeNoteByGrammarId:knowledge.grammarId];
                [_noteArray removeObjectAtIndex:_deleteIndex];
                [_noteTableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:[NSIndexPath indexPathForRow:_deleteIndex inSection:0]] withRowAnimation:UITableViewRowAnimationFade];
                [self updateButtonView];
            }
                break;
            default:
                break;
        }
        
    }
}

//-(NSString *)tableView:(UITableView *)tableView titleForDeleteConfirmationButtonForRowAtIndexPath:(NSIndexPath *)indexPath
//{
//    if (_tableViewType == NoteTableViewStyleStudy) {
//        return @"归档";
//    }else {
//        return @"删除";
//    }
//}
//
//- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
//{
//    
//    
//    
//}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    KnowledgeDetailViewController *viewController = [[KnowledgeDetailViewController alloc] initWithNibName:@"KnowledgeDetailViewController" bundle:nil];
    viewController.currentGrammar = [_noteArray objectAtIndex:indexPath.row];
    [self.navigationController pushViewController:viewController animated:YES];
    [viewController release];
    
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
}
- (void)archiveKnowledgeWithKnowledge:(Grammar*)knowledge
{
    [GrammarManager sharedManager].grammarType = @"archive";
    knowledge.state = @"1";
    [[GrammarManager sharedManager] addGrammar:knowledge];
    [self updateButtonView];
}

- (void)reviewKnowledgeWithGrammar:(Grammar*)knowledge
{
    [GrammarManager sharedManager].grammarType = @"review";
    knowledge.state = @"0";
    [[GrammarManager sharedManager] addGrammar:knowledge];
    [self updateButtonView];
}

- (void)viewDidUnload {
    [self setReviewButton:nil];
    [self setArchiveButton:nil];
    [self setSelectImageView:nil];
    [self setNoteTableView:nil];
    [super viewDidUnload];
}
@end
